﻿Public Class TaskTypeForm
    Enum action
        insert
        update
    End Enum

    Dim currentAction As action


    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        Me.Close()
    End Sub

    Private Sub cmdGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrabar.Click
        Dim taskType As TaskType = Nothing
        If Me.validateMandatoryItems Then
            If currentAction = action.insert Then
                taskType = New TaskType(Nothing, Me.txtNombre.Text, Me.txtDescripcion.Text)
                GenericAdministrator.insert(taskType)
            Else
                taskType = New TaskType(Convert.ToInt16(Me.txtId.Text), Me.txtNombre.Text, Me.txtDescripcion.Text)
                GenericAdministrator.update(taskType)
            End If
            Me.Close()
            Dim SearchTaskTypeForm As New SearchTaskTypeForm
            SearchTaskTypeForm.Show()
        End If
    End Sub

    Private Sub TaskTypeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New(ByVal id As Integer)

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        loadFormToEdition(id)
    End Sub

    Public Sub New()

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        currentAction = action.insert
    End Sub


    Private Sub clear_form()
        Me.txtId.Text = ""
        Me.txtNombre.Text = ""
        Me.txtDescripcion.Text = ""
    End Sub


    Private Function validateMandatoryItems() As Boolean
        If Me.txtNombre.Text = Nothing Then
            MsgBox("Debe ingresar un nombre para el tipo de tarea.")
            Return False
        Else
            Return True
        End If
    End Function

    Public Sub loadFormToEdition(ByVal id As Integer)
        currentAction = action.update

        Dim taskType As New TaskType
        taskType.idProperty = id
        taskType = GenericAdministrator.searchDataById(taskType)
        Me.txtId.Text = id
        Me.txtNombre.Text = taskType.nameProperty
        Me.txtDescripcion.Text = taskType.descriptionProperty
    End Sub

End Class